package me.zfz.base.dao.impl;

import java.util.List;
import java.util.Map;

import me.zfz.base.exception.DaoException;
import org.hibernate.Query;

import com.mixsmart.utils.StringUtils;

import me.zfz.base.dao.IExecuteDao;

/**
 * 执行Dao实现类
 *
 * @author author
 */
public abstract class ExecuteDaoImpl extends CommonDaoImpl implements IExecuteDao {

    @Override
    public Integer executeHql(String hql) throws DaoException {
        int result = 0;
        if (StringUtils.isEmpty(hql)) {
            return result;
        }
        log.info("执行HQL[" + hql + "]");
        try {
            result = getQuery(hql, null, false).executeUpdate();
        } catch (Exception e) {
            log.info("执行HQL[" + hql + "]--[异常]--[" + e.getMessage() + "]");
            e.printStackTrace();
            throw new DaoException(e.getLocalizedMessage(), e.getCause());
        }
        return result;
    }

    @Override
    public Integer executeHql(String hql, Map<String, Object> param) throws DaoException {
        int result = 0;
        if (StringUtils.isEmpty(hql)) {
            return result;
        }
        try {
            log.info("执行HQL[" + hql + "]");
            Query query = getQuery(hql, param, false);
            result = query.executeUpdate();

        } catch (Exception e) {
            log.info("执行HQL[" + hql + "]--[异常]--[" + e.getMessage() + "]");
            e.printStackTrace();
            throw new DaoException(e.getLocalizedMessage(), e.getCause());
        } finally {
            param = null;
        }
        return result;
    }

    @Override
    public Integer executeHql(String hql, List<Map<String, Object>> params) throws DaoException {
        int result = 0;
        if (StringUtils.isNotEmpty(hql)) {
            log.info("执行HQL[" + hql + "]");
            try {
                if (null != params && params.size() > 0) {
                    for (Map<String, Object> param : params) {
                        result += getQuery(hql, param, false).executeUpdate();
                    }
                }
            } catch (Exception e) {
                log.info("执行HQL[" + hql + "]--[异常]--[" + e.getMessage() + "]");
                e.printStackTrace();
                throw new DaoException(e.getLocalizedMessage(), e.getCause());
            } finally {
                params = null;
            }
        }
        return result;
    }

}
